<?php
namespace app\index\controller;

use app\BaseController;
use app\index\model\User as UserModel;
use think\facade\Event;
use think\facade\Db;

/**
 *
 * 多应用模式需绑定模块域名 config/app.php domain_bind
 * Class Index
 * @package app\index\controller
 */
class Index extends BaseController
{
    //控制器中间件 config/middleware.php别名配置
    protected $middleware = ['check'];

    public function initialize(){
        parent::initialize();
    }

    /**
     * http://local.tp6.com
     * @param UserModel $user2
     */
    public function index(UserModel $user2)
    {
        dump('当前应用名:'.app('http')->getName());

        echo "--------------------容器依赖注入-------------------------";
        dump($user2->info());

        $user = invoke('app\index\model\User');
        dump($user->username);

        echo "--------------------对象化调用-------------------------------";
        //读取配置文件
        dump(app()->config->get('database'));
        //使用session
        app()->session->set('user_name','张三');
        dump(app()->session->get('user_name'));

        echo "--------------------门面--------------------------------";
        dump( \app\facade\Test::hello('thinkphp') );

        echo "--------------------服务--------------------------------";
        dump(app()->user->boot());
        
			
		echo "--------------------协程--------------------------------";

		go(function () {
			for ($i = 0; $i < 2; $i++)
		    {
		    	$user = invoke('app\index\model\User');
		        $time = time();
				$user->save([
				    'username'  =>  $i."_swoole_".$time,
				    'password' =>  $time,
				    'create_time' =>  $time,
				]);
		    }
		});
		
		
		$data = ['name'=>'swoole'];
		go(function () use ($data) {
		   var_dump($data);
		   echo "<pre>";
		});
		
		
		$chan = new \chan(2);

        go(function () use ($chan)
        {
        	defer(function () {
        		echo "<pre>";
		        var_dump( "~a" );
		    });
		    
			$result = [];
		    for ($i = 0; $i < 2; $i++)
		    {
		        $result += $chan->pop();
		    }
		    
		    echo "<pre>";
		    var_dump($result);
		});
		
		go(function () use ($chan) {
		   $chan->push(['a' => 1]);
		});
		
		go(function () use ($chan) {
		   $chan->push(['b' => 2]);
		});
		
		echo "--------------------延迟任务--------------------------------";
		
		go(function () {
		    echo "a";
		    defer(function () {
		        echo "~a";
		    });
		    echo "b";
		    defer(function () {
		        echo "~b";
		    });
		    sleep(1);
		    echo "c";
		});
		
    }
    
    //定时器
    public function timer(){
    	$redis = new \Redis();
		$redis->connect('127.0.0.1', 6379);
		if(!$redis->get('timer1')){
			$timer = swoole_timer_tick(1000, function ($timer_id) {
			    echo "tick-1000ms\n";
			});
			$redis->set('timer1',$timer);
		}
	
		swoole_timer_after(5000, function () 
		{ 
			echo "after 5000ms.\n"; 
		}); 
    }
    
    //清除定时器
    public function cTimer(){
		$redis = new \Redis();
		$redis->connect('127.0.0.1', 6379);
		$timer1 = $redis->get('timer1');
		if($timer1){
			swoole_timer_clear($timer1);
			$redis->set('timer1',false);
		}
    }
    
    //重载定时器
    public function reloadTimer(){
    	$this->cTimer();
    	$this->timer();
    }

    /**
     * http://localhost?name=think
     * app/middleware/Check
     * config/middleware.php
     */
    public function think(){
        dump( "中间件跳转至 index/think 方法" );
    }

    /**
     * /hello?name=xxx
     * @param $name
     */
    public function hello($name){
        echo "hello $name !";
    }

    /**
     * 登录
     * @param UserModel $user
     */
    public function login(userModel $user){
        dump('开始登录.');
        //登录事件调用
        Event::trigger('UserLogin',$user->info());
    }

    /**
     * 退出登录
     * @param UserModel $user
     */
    public function loginout(userModel $user){
        dump('退出登录');
        //退出登录事件调用
        Event::trigger('onUserLogout',$user->info());
    }
    
    
    public function swoole(){
    
  //  	go(function () 
		// {
		//     sleep(1);
		//     dump("bbb");
		// });
		
	    dump( "swoole" );
		
    }
    



}
